package cn.bdqn.controller;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import cn.bdqn.entity.Opinion;
import cn.bdqn.entity.Repair;
import cn.bdqn.entity.UsersAndOpinion;
import cn.bdqn.service.OpinionService;
import cn.bdqn.util.Page;

@Controller
@RequestMapping("opinion")
public class OpinionController {
	@Autowired
	private OpinionService service;
	
	@RequestMapping("saveOpinion")
	public String saveOpinion(Integer _userid,HttpSession session, HttpServletRequest request,Opinion opinion,Model model,@RequestParam(value="a_idPicPath2",required=false) MultipartFile attach) {
		if(opinion.getNotname()==null) {
			opinion.setNotname(1);
		}else {
			opinion.setNotname(2);
		}
		String postimg=null;
		opinion.set_userid(_userid);
		//判断文件是否为空
		if(!attach.isEmpty()) {
			//定义文件上传路径
			String path=request.getSession().getServletContext().getRealPath("statics"+File.separator+"images");
			String oldFileName=attach.getOriginalFilename();
			String prefix=FilenameUtils.getExtension(oldFileName);
			int filesize=5000000;
			if(attach.getSize()>filesize) {
				request.setAttribute("message", "* 文件大小不得超过500k");
				return "main";
			}else if(prefix.equalsIgnoreCase("jpg")||prefix.equalsIgnoreCase("jpeg")||prefix.equalsIgnoreCase("png")||prefix.equalsIgnoreCase("pneg")) {
				//获取当前系统时间和随机数+"_Personal.jpg"
				String fileName=System.currentTimeMillis()+RandomUtils.nextInt(1000000)+"_Personal.jpg";
				File targetFile=new File(path, fileName);
				if(!targetFile.exists()) {
					targetFile.mkdirs();
				}
				try {
					attach.transferTo(targetFile);
				} catch (Exception e) {
					e.printStackTrace();
					request.setAttribute("message", "* 上传失败");
					return "main";
				}
				postimg="\\dormitory\\statics\\images"+File.separator+fileName;
			}else {
				request.setAttribute("message", "* 上传图片格式必须是jpg、jpeg、png、pneg");
				return "main";
			}
		}
		opinion.setContextimg(postimg);
		Integer count=service.saveOpinion(opinion);
		return "forward:/users/page";
	}
	@RequestMapping("queryAllOpinion")
	public String queryAllOpinion(Integer userid,Model model) {
		List<UsersAndOpinion> list=service.queryAllOpinion(userid);
		model.addAttribute("list", list);
		return "detialOpinion";
	}
	@RequestMapping("deleteOpinion")
	public String deleteOpinion(Integer opinionid,Integer userid) {
		Integer num=service.deleteOpinionById(opinionid);
		return "forward:/opinion/queryAllOpinion?userid="+userid;
	}
	//管理建议
	@RequestMapping("toguanopinion")
	public String toguanopinion(String context,Integer pageNo,Integer pageSize,Model model) {
		if(pageNo==null) {
			pageNo=1;
		}
		if(pageSize==null) {
			pageSize=3;
		}
		if(context==null) {
			context="";
		}
		Page<UsersAndOpinion> page=new Page<>();
		page.setPageSize(pageSize);
		page.setPageNo(pageNo);
		model.addAttribute("context", context);
		service.queryAllOpinionbyguan(context, page);
		model.addAttribute("page", page);
		return "guanOpinion";
	}
	@RequestMapping("updateOpinionStatus")
	public String updateOpinionStatus(Integer pageNo,Opinion opinion) {
		if(opinion.getStatus()==1) {
			opinion.setStatus(2);
		}else if(opinion.getStatus()==2) {
			opinion.setStatus(1);
		}
		Integer num=service.updateOpinionStatus(opinion);
		return "forward:/opinion/toguanopinion?pageNo="+pageNo;
	}
	//查看详情
	@RequestMapping("toguandetailopinion")
	public String toguandetialopinion(Integer opinionid,Model model) {
		UsersAndOpinion opinion=service.queryOpinionByOpinionid(opinionid);
		model.addAttribute("opinion", opinion);
		return "toguandetailopinion";
	}
}
